require 'rest-client'
require 'json'

class RubocopAnalysis
  def initialize(head_sha)

    @repo = "liwen/gitos"
    @access_token = "2339922d27630f723f566e9a2ace6a6f"
    @head_sha = head_sha
  end

  def exec
    puts "#{@repo}, #{@access_token}, #{@head_sha}"
    create_rubocop
    rubocop_exec
  end

  def create_rubocop
    puts "create_rubocop"
    data = {
      details_url: 'https://gitee.com/liwen',
      name: 'liwen_rubocop',
      access_token: @access_token,
      head_sha: @head_sha
    }
    url = "https://gitee.com/api/v5/repos/#{@repo}/check-runs"
    @response_json = RestClient.post url, data
    @response = JSON.parse(@response_json)
    @check_run_id = @response['id']
    puts "@check_run_id => #{@check_run_id}"
  end

  def progress_check_run
    puts "progress_check_run"
    url = "https://gitee.com/api/v5/repos/#{@repo}/check-runs/#{@check_run_id}"
    data = {
      access_token: @access_token,
      status: 'in_progress'
    }
    RestClient.patch url, data
  end

  def analysis_annotation_conclusion(output)
    puts "analysis_annotation_conclusion"
    annotations = []
    conclusion = 'success'
    annotation_level_list = {"warning" => "warning", "error" => "failure", "fatal" => "failure"}
    output['files'].each do |file|

      file['offenses'].each do |offense|
        conclusion = "failure" if conclusion == 'success' && ["error", "fatal"].include?(offense["severity"])
        start_line       = offense['location']['start_line']
        end_line         = offense['location']['last_line']
        start_column     = offense['location']['start_column']
        end_column       = offense['location']['last_column']
        message          = offense['message']
        path             = offense['path']
        annotation_level = annotation_level_list[offense["severity"]]

        annotation = {
          path: path,
          start_line: start_line,
          end_line: end_line,
          annotation_level: annotation_level,
          message: message
        }

        if start_line == end_line
          annotation.merge(start_column: start_column, end_column: end_column)
        end

        annotations.push(annotation)
      end
    end
    [annotations, conclusion]
  end

  def rubocop_exec
    progress_check_run
    puts "rubocop_exec"

    @report = `rubocop --format json`
    puts "rubocop format json"
    @output = JSON.parse @report

    annotations, conclusion = analysis_annotation_conclusion(@output)

    puts "summary"
    summary = "Octo RuboCop summary\n-Offense count: #{@output['summary']['offense_count']}\n-File count: #{@output['summary']['target_file_count']}\n-Target file count: #{@output['summary']['inspected_file_count']}"
    text = "Octo RuboCop version: #{@output['metadata']['rubocop_version']}"

    url = "https://gitee.com/api/v5/repos/#{@repo}/check-runs/#{@check_run_id}"
    puts "data"
    data = {
      access_token: @access_token,
      conclusion: conclusion,
      details_url: "https://gitee.com/liwen",
      output: {
        title: 'Octo RuboCop',
        summary: summary,
        text: text,
        annotations: annotations[1..50]
      }
    }
    puts "#{url}"
    response = RestClient.patch url, data
    puts response
  end
end

inputted_strings = ARGV
puts "You inputted: "
if ARGV.empty?
  puts "nothing :("
else
  inputted_strings.each do |str|
    puts str
    RubocopAnalysis.new(str).exec
  end
end